package com.hy.demo.ui.list;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.hy.demo.R;
import com.hy.demo.adapter.RcyAdapter;
import com.hy.demo.app.BaseActivity;
import com.hy.demo.widget.MixtureRecyclerView;
import com.hy.demo.widget.MixtureScrollView;
import com.hy.frame.adapter.IAdapterListener;
import com.hy.frame.recycler.BaseAdapter;
import com.hy.frame.recycler.BaseHolder;
import com.hy.frame.util.ToastUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;


public class MixtureActivity extends BaseActivity implements IAdapterListener<String> {
    private SmartRefreshLayout vRefresh;
    private FrameLayout layPage;
    private MixtureScrollView vScroll;
    private LinearLayout layContainer;
    private LinearLayout layPager;
    private TabLayout vTab;
    private ViewPager2 vPager;
    private List<String> mTabs;

    @Override
    protected int getLayoutId() {
        return R.layout.v_list_mixture_refresh;
    }

    @Override
    protected void initView() {
        vRefresh = findViewById(R.id.v_refresh);
        layPage = findViewById(R.id.lay_page);
        vScroll = findViewById(R.id.v_scroll);
        layContainer = findViewById(R.id.lay_container);
        layPager = findViewById(R.id.lay_pager);
        vTab = findViewById(R.id.v_tab);
        vPager = findViewById(R.id.v_pager);
    }

    @Override
    protected void initData() {
        setTitle(R.string.menu_rcy_mixture);
        getToolbarUI().setRightText("菜单");
        requestData();
        updatePagerUI();
        vScroll.post(new Runnable() {
            @Override
            public void run() {
                //纠正首次自动滚动
                vScroll.scrollTo(0, 0);
                //检测是否有Tab布局
                vScroll.checkTab();
            }
        });

    }

    public void requestData() {
        mTabs = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mTabs.add("菜单" + (i + 1));
        }
    }

    public void updatePagerUI() {
        vPager.setAdapter(new ViewPagerAdapter(getContext(), mTabs));
    }

    @Override
    public void onViewClick(View v, String item, int position) {
        ToastUtil.show(this, item);
    }

    @Override
    public void onRightClick() {

    }

    static class ViewPagerAdapter extends BaseAdapter<String> {
        public ViewPagerAdapter(@NonNull Context cxt) {
            super(cxt);
        }

        public ViewPagerAdapter(@NonNull Context cxt, @Nullable List<String> datas) {
            super(cxt, datas);
        }

        public ViewPagerAdapter(@NonNull Context cxt, @Nullable List<String> datas, @Nullable IAdapterListener<String> listener) {
            super(cxt, datas, listener);
        }

        @Override
        public int getItemLayoutId() {
            return 0;
        }

        @NonNull
        @Override
        public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RecyclerView rcyList = new MixtureRecyclerView(getContext());
            rcyList.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            rcyList.setLayoutManager(new GridLayoutManager(getContext(), 3));
            return new BaseHolder(rcyList);
        }

        @Override
        public void bindItemData(BaseHolder holder, int position) {
            RecyclerView rcyList = (RecyclerView) holder.itemView;
            if (rcyList.getAdapter() == null) {
                List<String> datas = new ArrayList<>();
                for (int i = 0; i < 64; i++) {
                    datas.add("测试" + (i + 1));
                }
                RcyAdapter adapter = new RcyAdapter(getContext(), datas);
                rcyList.setAdapter(adapter);
            }
        }
    }
}
